package com.bwie.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bwie.user.entity.GoodsEntity;
import com.bwie.user.vo.ActivityVo;
import org.apache.commons.lang.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.user.dao.ActivityDao;
import com.bwie.user.entity.ActivityEntity;
import com.bwie.user.service.ActivityService;


@Service("activityService")
public class ActivityServiceImpl extends ServiceImpl<ActivityDao, ActivityEntity> implements ActivityService {

        @Autowired
        private ActivityDao activityDao;
        @Autowired
        private RedisTemplate redisTemplate;
        @Autowired
        private RedissonClient client;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ActivityEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        String page1 = (String) params.get("page");
        BoundHashOperations voList = redisTemplate.boundHashOps("voList2");
        IPage<ActivityEntity> list = (IPage<ActivityEntity>) voList.get(page1);
        if (list!=null){
            System.out.println("缓存命中");
            return new PageUtils(list);
        }else {
            RLock lock = client.getLock("lock");
            try {
                boolean b = lock.tryLock(10, TimeUnit.MINUTES);
                if (b){
                    IPage<ActivityEntity> page = this.page(
                            new Query<ActivityEntity>().getPage(params),
                            wrapper
                    );
                    //取出来
                    voList.put(page1,page);
                    //缓存时间
                    voList.expire(30,TimeUnit.SECONDS);
                    System.out.println("没命中");
                    return new PageUtils(page);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }

//        if (StringUtils.isNotEmpty(key)){
//            wrapper.eq("goods_id",key);
//        }
//
//
//        IPage<ActivityEntity> page = this.page(
//                new Query<ActivityEntity>().getPage(params),
//                wrapper
//        );

        return null;
    }

    //把list转换成json数组
    //JSONArray jsonArray = JSONObject.parseArray(JSON.toJSONString(deviceError));
    ////把json数组转换成String类型
    //String json = jsonArray.toJSONString();

    @Override
    public List<ActivityVo> voList(String goodsName) {
        //优化----将活动列表存入redis,做缓存查询，增加查询效率
        List<ActivityVo> voList = (List<ActivityVo>) redisTemplate.boundHashOps("lie").get("voList");
        //先获取redis中有无这个列表的缓存
        if (goodsName!=null&&!goodsName.equals("")){
            //数据库查找
            List<ActivityVo> vos = activityDao.voList(goodsName);
            System.out.println("模糊---------");
            return vos;
        }

        if (voList!=null){

            //volist不为null 证明查到了
            System.out.println("缓存命中vo");
            return voList;
        }else{
            //判断为空，缓存中没有，就存入redis
            RLock lock = client.getLock("lock");
//            try {

            try {
                boolean  b = lock.tryLock(10, TimeUnit.MILLISECONDS);

                if (b){
                    int i=0;
                    List<ActivityVo> vos = activityDao.voList(goodsName);
                    redisTemplate.boundHashOps("lie").put("voList",vos);
                    redisTemplate.expire("lie",60,TimeUnit.SECONDS);
                    i++;
                    System.out.println("无命中次数"+i);
                    return vos;
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }
        return null;

    }

//    @Override
//    public PageUtils voList2(Map<String, Object> params) {
//        QueryWrapper<ActivityEntity> wrapper = new QueryWrapper<>();
//        String key = (String) params.get("key");
//        if (StringUtils.isNotEmpty(key)){
//            wrapper.like("goods_name",key);
//        }
//        IPage<ActivityEntity> page = this.page(new Query<ActivityEntity>().getPage(params),wrapper);
//
//        return new PageUtils(page);
//    }

    public static void main(String[] args) {
        System.out.println("Runtime.getRuntime().availableProcessors() = " + Runtime.getRuntime().availableProcessors());
    }

}